package colaborativo.ondefica;

import org.json.JSONArray;
import org.json.JSONException;

import android.app.Activity;
import android.app.AlertDialog;
import android.graphics.drawable.ColorDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class Evento extends Activity {
	
	TextView datainicio, datafim, horainicio, horafim,titulo,descricao,contato,local;
	private int id;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_evento);
		
		Bundle extras = getIntent().getExtras();
		if(extras!=null){
			id = extras.getInt("idEv");		
			Log.d("Ev.Debug", Integer.toString(id));
		}
		 datainicio = (TextView) findViewById(R.id.recebedatainicio);
		 datafim = (TextView) findViewById(R.id.recebedatafim);
		 horainicio = (TextView) findViewById(R.id.recebehorarioinicio);
		 horafim = (TextView) findViewById(R.id.recebehorariofim);
		 titulo = (TextView) findViewById(R.id.recebetitulo);
		 descricao = (TextView) findViewById(R.id.recebedescricao);
		 contato = (TextView) findViewById(R.id.recebecontato);
		 local = (TextView) findViewById(R.id.recebelocal);
		 new MyAsyncTask().execute("Vaii");
		 
	}
	
	private AlertDialog teladialogo;
	public void Login(View v) {
		
		LayoutInflater li = getLayoutInflater();
		View view = li.inflate(R.layout.activity_logar, null);
				
		view.findViewById(R.id.entrar).setOnClickListener(new View.OnClickListener() 
		{ 
			public void onClick(View arg0) {
			Toast.makeText(Evento.this, 
					"Logado", Toast.LENGTH_SHORT).show();
				} 
		});
		
		AlertDialog.Builder builder = new AlertDialog.Builder(this); 
		
		builder.setView(view); 
		teladialogo = builder.create(); 
		teladialogo.getWindow().setBackgroundDrawable(new
				ColorDrawable(android.graphics.Color.TRANSPARENT));
		teladialogo.show();
	
			
	}
	
	
	
	private class MyAsyncTask extends AsyncTask<String, Integer, JSONArray>{
    	@Override
        protected JSONArray doInBackground(String... params) {	        	
        	
        	
        	String qry= "SELECT * FROM `geoPonto` INNER JOIN ( SELECT * FROM `evento` "
        			+ "WHERE `idEvento`="+id+") as EV on `EV`.`idGeoPonto` = `geoPonto`.`idLocal`";
    		
        	Log.d("oM.pV.Query", qry);	    		
    		return DbConnection.consulta(qry);	    				        
        }
         
        protected void onPostExecute(JSONArray result){
        	Log.d("oM.pV.Result", "Acabou consulta");
        	evento(result);
			
        	  			  
        }                
        
    }
	
	
	private void evento(JSONArray evento){		
		try {
			titulo.setText(evento.getJSONObject(0).getString("titulo"));
			descricao.setText(evento.getJSONObject(0).getString("descricao"));
			datafim.setText(evento.getJSONObject(0).getString("dataFim"));
			datainicio.setText(evento.getJSONObject(0).getString("dataInicio"));
			horainicio.setText(evento.getJSONObject(0).getString("horarioInicio"));
			horafim.setText(evento.getJSONObject(0).getString("horarioFim"));
			local.setText(evento.getJSONObject(0).getString("nomeLocal"));
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.evento, menu);
		return true;
	}

}
